package com.xzlf;

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class Audio implements Runnable{

    private Clip clip;
    private byte[] b ;
    private AudioInputStream audioInputStream;

    public Audio(byte[] b) {
        this.b = b;
    }

    @Override
    public void run() {
        try {
//        b = Arrays.copyOf(ResourceMgr.bytes,ResourceMgr.bytes.length);
        audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(new ByteArrayInputStream(b)));
        clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        } catch (LineUnavailableException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedAudioFileException e) {
            throw new RuntimeException(e);
        }
        clip.start();
//        clip.addLineListener(event -> {
//            if (event.getType() == LineEvent.Type.STOP)
//                clip.close();
//        });
    }

}
